// 最小覆盖子串    s= abcdefa  t= acd  =》 输出：abcd

var minWindow = function (s, t) {
  let len = s.length
  let result = s
  let flag = false
  for (let i = 0, j = 0; j < len; j++) {
    let str = s.slice(i, j + 1)
    while (contain(str, t)) {
      flag = true
      result = str.length < result.length ? str : result
      i = i + 1
      str = s.slice(i, j + 1)
    }
  }

  return flag ? result : ""
};

function contain(str, t) {
  if (str.length < t.length) return false
  let mapA = new Map()
  for (let i = 0; i < str.length; i++) {
    if (mapA.has(str[i])) {
      mapA.set(str[i], mapA.get(str[i]) + 1)
    } else {
      mapA.set(str[i], 1)
    }
  }
  for (let i = 0; i < t.length; i++) {
    if (!mapA.has(t[i]) || mapA.get(t[i]) === 0) return false
    mapA.set(t[i], mapA.get(t[i]) - 1)
  }

  return true
}


// console.log(minWindow('abcdefa', 'acd'));


contain('abcsab', 'abs')
